<template>
    <div>
        <div class="container">
            <div class="handle-box">
                <el-form :model="query" ref="queryForm" :inline="true" label-width="68px">
                    <el-form-item label="角色名称" prop="name">
                        <el-input v-model="query.roleName" size="mini" placeholder="角色名称" class="handle-input mr10"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="cyan" size="mini" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                    </el-form-item>
                </el-form>
                <div>
                    <el-row :gutter="10" class="mb8 el-form-item">
                        <el-col :span="1.5">
                            <el-button type="primary" icon="el-icon-plus" class="handle-del floatr" size="mini" @click="addSysRole">新增</el-button>
                        </el-col>
                    </el-row>
                </div>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="roleName" label="角色名称"></el-table-column>
                <el-table-column  label="创建时间" >
                    <template slot-scope="scope">{{scope.row.createTime| dateFormat}}</template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-delete"
                                class="red"
                                @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="角色名称">
                    <el-input maxlength="20" v-model="form.roleName" ></el-input>
                </el-form-item>
                <el-form-item label="菜单权限">
                     <el-tree
                        ref="tree"
                        :check-strictly="checkStrictly"
                        :data="menuList"
                        :props="defaultProps"
                        show-checkbox
                        node-key="id"
                        class="permission-tree"
                    />
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="butCancel">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import sysRole from '../../api/sysRole';
    import sidebar from '../../api/sidebar';
    export default {
        name: 'basetable',
        data() {
            return {
                query: {
                    roleName: '',
                    status:'',
                    id:'',
                    version:'',
                    pageIndex: 1,
                    pageSize: 10
                },
                tableData: [],
                multipleSelection: [],
                menuList:[],
                delList: [],
                editVisible: false,
                pageTotal: 0,
                form: {
                    menu:[],
                    roleName:""
                },
                checkStrictly: false,
                defaultProps: {
                    children: 'childMenus',
                    label: 'name'
                },
                idx: -1,
                id: -1
            };
        },
        created() {
            this.getData();
        },
        filters:{
            //格式化后台传过来的时间戳
            dateFormat(time) {
                let date = new Date(time);
                return vue.$tool._dateFormat(date,'yyyy-MM-dd hh:mm')
            }
        },
        methods: {
            // 获取list
            getData() {
                sysRole.selectList(this.query).then(res => {
                    this.tableData = res.data.items;
                    this.pageTotal = res.data.total || 50;
                });
            },
            // 获取菜单list
            getmenuList(id){
                var _that =this;
                sidebar.selectMenu({}).then(res => {
                    _that.menuList = res.data;
                    if(id != "" && id != null){
                        sysRole.selectSysRoleMenu(id).then(res => {
                            for (var i = 0; i < res.data.items.length; i++) {
                                var flag = false;
                                for (var j = 0; j < _that.menuList.length; j++) {
                                    if(res.data.items[i].menuId == _that.menuList[j].id){
                                        flag = true;
                                    }
                                }
                                if(!flag){
                                    _that.form.menu.push(res.data.items[i].menuId);
                                }
                            }
                            this.$refs.tree.setCheckedKeys(_that.form.menu); //设置目前勾选的节点
                        });
                    }
                });
            },
            // 查询角色
            selectSysRoleOne(id){
                var _that =this;
                sysRole.selectSysRole(id).then(res => {
                    _that.form = res.data;
                    if(_that.form.menu == null){
                        _that.form.menu = [];
                    }
                    this.getmenuList(id)
                });
            },
            // 触发搜索按钮
            handleSearch() {
                this.$set(this.query, 'pageIndex', 1);
                this.getData();
            },
            // 删除操作
            handleDelete(index, row) {
                // 二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.query.status = 1 ;
                    this.query.id = row.id ;
                    this.query.version = row.version ;
                    sysRole.delSysRole(this.query).then(res => {
                        this.$message.success('删除成功');
                        this.getData();
                    });
                })
                .catch(() => {});
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 编辑操作
            handleEdit(index, row) {
                this.idx = index;
                this.editVisible = true;
                this.selectSysRoleOne(row.id)
            },
            //新增操作
            addSysRole() {
                vue.$tool.resetForm(this.form);
                this.$refs.tree.setCheckedKeys([]); //设置目前勾选的节点
                this.editVisible = true;
                this.getmenuList("");
            },
            // 保存编辑
            saveEdit() {
                var menu = this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHalfCheckedKeys());
                this.form.menu = menu;
                if(this.form.id != null && this.form.id != undefined){
                    // 编辑
                    sysRole.updateSysRole(this.form).then(res => {
                        this.$message.success("保存成功");
                        this.getData();
                    });
                }else{
                    // 新增
                    sysRole.addSysRole(this.form).then(res => {
                        this.$message.success("保存成功");
                        this.getData();
                    });
                }
                this.butCancel();
            },
            butCancel(){
                this.editVisible = false;
                this.$refs.tree.setCheckedKeys([]); //设置目前勾选的节点
                vue.$tool.resetForm(this.form);
            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getData();
            }
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
    .floatr {
        float: right;
    }
    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>
